;-----------------------------------------------------------
; Pletter v0.5c ROM/RAM Depacker v1.0
; adapted asMSX code by Pepe Vila
; HL = RAM/ROM source compressed data 
; DE = RAM/ROM destination uncompressed data
; changes all registers
;-----------------------------------------------------------
unpack:
	ld	a,[hl]
	inc	hl
	exx
	ld	de,0
	add	a,a
	inc	a
	rl	e
	add	a,a
	rl	e
	add	a,a
	rl	e
	rl	e
	ld	hl,modes
	add	hl,de
	ld	e,[hl]
	ld	ixl,e
	inc	hl
	ld	e,[hl]
	ld	ixh,e
	ld	e,1
	exx
	ld	iy,loop

; Main depack loop
literal:
	ldi
loop:
	add	a,a
	call	z,getbit
	jr	nc,literal
; Compressed data
	exx
	ld	h,d
	ld	l,e
getlen:
	add	a,a
	call	z,getbitexx
	jr	nc,lenok
lus:
	add	a,a
	call	z,getbitexx
	adc	hl,hl
	ret	c
	add	a,a
	call	z,getbitexx
	jr	nc,lenok
	add	a,a
	call	z,getbitexx
	adc	hl,hl
	ret	c
	add	a,a
	call	z,getbitexx
	jp	c,lus
lenok:
	inc	hl
	exx
	ld	c,[hl]
	inc	hl
	ld	b,0
	bit	7,c
	jp	z,offsok
	jp	ix

mode7:
	add	a,a
	call	z,getbit
	rl	b
mode6:
	add	a,a
  	call	z,getbit
	rl	b
mode5:
	add	a,a
	call	z,getbit
	rl	b
mode4:
	add	a,a
	call	z,getbit
	rl	b
mode3:
	add	a,a
	call	z,getbit
	rl	b
mode2:
	add	a,a
	call	z,getbit
	rl	b
	add	a,a
	call	z,getbit
	jr	nc,offsok
	or	a
	inc	b
	res	7,c

offsok:
	inc	bc
	push	hl
	exx
	push	hl
	exx
	ld	l,e
	ld	h,d
	sbc	hl,bc
	pop	bc
	ldir
	pop	hl
	jp	iy

getbit:
	ld	a,[hl]
	inc	hl
	rla
	ret

getbitexx:
	exx
	ld	a,[hl]
	inc	hl
	exx
	rla
	ret

modes:
	dw	offsok
	dw	mode2
	dw	mode3
	dw	mode4
	dw	mode5
	dw	mode6






;-----------------------------------------------------------
; Pletter v0.5b VRAM Depacker v1.1 - 16 Kb version
; HL = RAM/ROM source
; DE = VRAM destination
;-----------------------------------------------------------
; Copied from Pepe Vila Cudillero tutorial
DEPLET:

	di
; VRAM address setup
	ld a,e
	;---
	nop
	nop
	;---

	out (099h),a
	;---
	nop
	nop
	;---
	ld a,d
	or 040h
	out (099h),a
	;---
	nop
	nop
	;---
; Initialization
	ld a,[hl]
	inc hl
	exx
	ld de,0
	add a,a
	inc a
	rl e
	add a,a
	rl e
	add a,a
	rl e
	rl e
	ld hl,modes2
	add hl,de
	ld e,[hl]
	ld ixl,e
	inc hl
	ld e,[hl]
	ld ixh,e
	ld e,1
	exx
	ld iy,loop2
; Main depack loop
literal2: ld c,098h
	outi
	;---
	nop
	nop
	;---
	
	inc de
loop2: add a,a
	call z,getbit2
	jr nc,literal2
; Compressed data
	exx
	ld h,d
	ld l,e
getlen2: add a,a
	call z,getbitexx2
	jr nc,lenok2
lus2: add a,a
	call z,getbitexx2
	adc hl,hl
	ret c
	add a,a
	call z,getbitexx2
	jr nc,lenok2
	add a,a
	call z,getbitexx2
	adc hl,hl
	jp c,Depack_out2
	add a,a
	call z,getbitexx2
	jp c,lus2
lenok2: inc hl
	exx
	ld c,[hl]
	inc hl
	ld b,0
	bit 7,c
	jp z,offsok2
	jp ix
mode72: add a,a
	call z,getbit2
	rl b
mode62: add a,a
	call z,getbit2
	rl b
mode52: add a,a
	call z,getbit2
	rl b
mode42: add a,a
	call z,getbit2
	rl b
mode32: add a,a
	call z,getbit2
	rl b
mode22: add a,a
	call z,getbit2
	rl b
	add a,a
	call z,getbit2
	jr nc,offsok2
	or a
	inc b
	res 7,c
offsok2: inc bc
	push hl
	exx
	push hl
	exx
	ld l,e
	ld h,d
	sbc hl,bc
	pop bc
	push af
__loop2:
	ld a,l
	;---
	nop
	nop
	;---

	out (099h),a
	;---
	nop
	nop
	;---
	
	ld a,h
	nop ; VDP timing
	out (099h),a
	nop ; VDP timing
	;---
	nop
	nop
	;---
	
	in a,(098h)
	;---
	nop
	nop
	;---
	
	ex af,af'
	ld a,e
	nop ; VDP timing
	;---
	nop
	nop
	;---
	
	out (099h),a
	;---
	nop
	nop
	;---

	ld a,d
	or 040h
	out (099h),a
		;---
	nop
	nop
	;---

	ex af,af'
	nop ; VDP timing
	out (098h),a
		;---
	nop
	nop
	;---

	inc de
	cpi
	jp pe,__loop2
	pop af
	pop hl
	jp iy
getbit2: ld a,[hl]
	inc hl
	rla
	ret
getbitexx2:
	exx
	ld a,[hl]
	inc hl
	exx
	rla
	ret
; Depacker exit
Depack_out2:
	ei
	ret
modes2:
	dw offsok2
	dw mode22
	dw mode32
	dw mode42
	dw mode52
	dw mode62
	dw mode72





;HL ROM/RAM src
;DE VRAM dest
;BC return address
;interrupts must be disabled
unpack_rle_2_vram:
                ;setup vdp for transfer
				ex      de,hl
                ld      a,l
                out     (VDP_ADDR),a
                ld      a,h
                and     $3F
                or      $40
                out     (VDP_ADDR),a
				ex      de,hl				;hl intact
							
				ld		d,b
				ld		e,c					;de will hold return address
							
				;unpacking
				ld		a,[hl]
				ld		c,a					;counter token it's on c
unpack_rle_2_vram_loop:
				inc		hl
				ld		a,[hl]
				cp		c
				jr		nz,unpack_rle_2_vram_not_token_found
				;end?
				inc		hl
				ld		a,[hl]
				cp		c
				jr		z,unpack_rle_2_vram_end		;token was found twice in a row
				inc		hl
				ld		b,[hl]				;counter set
unpack_rle_2_vram_token_loop:
				out		(VDP_DATA),a
				nop
				djnz	unpack_rle_2_vram_token_loop
				jr		unpack_rle_2_vram_loop
				
unpack_rle_2_vram_not_token_found:
				out		(VDP_DATA),a
				nop
				jr		unpack_rle_2_vram_loop
								
unpack_rle_2_vram_end:
				ld		h,d
				ld		l,e					;restore return address saved on de
				
				jp		(hl)				;return





;eof
;-----------------------------------------------------------
